/**
 * Created by hcadmin on 16/4/13.
 */
/**
 * Created by hcadmin on 16/4/12.
 */
/**
 * Created by hcadmin on 16/4/6.
 */
define([
    "jquery",
    "IO",
    "bootbox"
], function ($, IO, bootbox) {

    $(function () {

        var year_1=1;
        var year_2=2;
        var year_3=3;
        var activity_mid=$("#activity_mid_hidden").val();
        var page_num=$(".page_control .li_num.selected").text();
        gettable(year_1,activity_mid,1);
        //查询
        $("#searchid1").bind("click", function () {
            gettable(year_1,activity_mid,1);
        });
        $("#searchid2").bind("click", function () {
            gettable(year_2,activity_mid,1);
        });
        $("#searchid3").bind("click", function () {
            gettable(year_3,activity_mid,1);
        });

        var act_id;
        //编辑
        $(document).on("click",".editaa",function(){
            act_id=$(this).attr("act_id");
            var act_name=$(this).attr("act_name");
            $("#textName").val(act_name);

        });

        $("#btnUpdate").click(function(){
            var quotiety=$("#selectgroupedit option:selected").attr("data-id");
            var user_mid=act_id;
            var year_id=$("#year_id_hidden").val();

            var redflag=$(".research_reslut").val();

            var params={};
            params.redflag=redflag;
            params.mid=user_mid;
            params.quotiety=quotiety;
            params.activity_mid=activity_mid;

            IO.post("/shapan/api/beltlineUpdate",params,function(d){
                if(d.code=="0"){
                    $('#myModal').modal('hide');
                    var page_num=$(".page_control .li_num.selected").text();
                    gettable(year_id,activity_mid,page_num);
                }
                else {
                    $('#myModal').modal('hide');
                    alert(d.msg);
                }
            })

        });

        $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
            // 获取已激活的标签页的名称
            var activeTab = $(e.target).text();
            // 获取前一个激活的标签页的名称
            if(activeTab=="第一年度"){
                $("#year_id_hidden").val("1");
                gettable(1,activity_mid,1);
            }
            else if(activeTab=="第二年度"){
                $("#year_id_hidden").val("2");

                gettable(2,activity_mid,1);
            }
            else
            {
                $("#year_id_hidden").val("3");
                gettable(3,activity_mid,1);
            }

        });

    });
    function gettable(year_id,activity_mid,page_num){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '姓名',
                dataIndex: 'user_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].user_name + "</span>";
                    //return "<span><img src='" + data[datarow].user_name + "' width='80px;' height='80px;'>&nbsp;&nbsp;</span>" + data[datarow].title + "</span>";
                }
            },
            {
                title: '公司',
                dataIndex: 'group_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].group_name==null){
                        return "<span>"+"请选择公司"+"</span>";
                    }
                    else {
                        return "<span>" +data[datarow].group_name + "</span>";
                    }

                }
            },
            {
                title: '现金回报(元)',
                dataIndex: 'quotiety',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].cash_return==null){
                        return '<span>'+"0"+'</span>';
                    }
                    else {
                        return '<span>'+data[datarow].cash_return+'</span>';
                    }

                }
            },
            {
                title: '回报率(%)',
                dataIndex: 'quotiety',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].roe==null){
                        return '<span>'+"0"+'</span>';
                    }
                    else {
                        return '<span>'+data[datarow].roe+'</span>';
                    }
                }
            }
        ];

        //数组初始化

        function dataInit(year_id,activity_mid,page_num) {

            grid = new Grid("/shapan/api/cashReturnShow", $("#table_my_toast"+year_id), $("#paging_my_toast"+year_id), page_num, 10, columns, get_input_param(year_id,activity_mid), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param(year_id,activity_mid) {

            var group_mid;

            if(year_id==1){
                group_mid= $("#selectgroup1 option:selected").attr("data-id");
            }
            else  if(year_id==2){
                group_mid= $("#selectgroup2 option:selected").attr("data-id");
            }
            else
            {
                group_mid= $("#selectgroup3 option:selected").attr("data-id");
            }
            if(group_mid=="undefined"){
                group_mid="";
            }
            var param="group_mid=" + group_mid+"&year_id="+year_id+"&activity_mid="+activity_mid;
            console.log("group_mid"+group_mid);
            return param;
        }

        dataInit(year_id,activity_mid,page_num);
    }

});